package com.bsh.repository;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import com.bsh.mybatis.IRepository.ModuleMapper;
import com.bsh.mybatis.model.Module;
import com.bsh.utils.Pager;
import com.mysql.jdbc.StringUtils;

@Repository
public class DaoModule{ 
	@Resource
	SqlSessionTemplate session;
	@Resource
	JdbcTemplate jdbcTemplate;
	@Resource
	NamedParameterJdbcTemplate namedJdbcTemplate;
	
	ModuleMapper getMapper(){
		return session.getMapper(ModuleMapper.class);
	}
	
	
	public int deleteByPrimaryKey(Integer id) {
		int res = getMapper().deleteByPrimaryKey(id);
		return res;
	}

	public int insert(Module record) {
		int res = getMapper().insert(record);
		return record.getId();
	}

	public int insertSelective(Module record) {
		int res = getMapper().insertSelective(record);
		return record.getId();
	}

	public Module selectByPrimaryKey(Integer id) {
		Module row = getMapper().selectByPrimaryKey(id);
		return row;
	}

	public int updateByPrimaryKeySelective(Module record) {
		int res = getMapper().updateByPrimaryKeySelective(record);
		return res;
	}

	public int updateByPrimaryKey(Module record) {
		int res = getMapper().updateByPrimaryKey(record);
		return res;
	}

	/**
	 * 查询所有模块
	 * @return
	 */
	public List<Module> getAllMoudle() {
		List<Module> list = getMapper().getAllMoudle();
		return list;    
	}

	/**
	 * 模块分页
	 */
	public List<Map<String, Object>> getPage(Pager pager, String where) {
		StringBuilder sb = new StringBuilder();
		MapSqlParameterSource ps = new MapSqlParameterSource();
		sb.append("SELECT v.*,pv.name level_name, u. name user_name,u.email,");
		sb.append("rp.name province_name,rc.name city_name,ra.name area_name ");
		sb.append("FROM partner v ");
		sb.append("JOIN region rp ON rp.id = v.province ");
		sb.append("JOIN region rc ON rc.id = v.city ");
		sb.append("JOIN region ra ON ra.id = v.area ");
		sb.append("join user u on u.id=v.id ");
		sb.append("join partner_level pv on pv.id=v.partner_level ");
		sb.append("where 1=1 ");
		if (!StringUtils.isNullOrEmpty(where)) {
			sb.append(where);
		}
		Integer count = namedJdbcTemplate.queryForInt("select count(0) from (" + sb.toString() + ")v", ps);

		sb.append(" order by v.id desc limit :start,:size");
		ps.addValue("start", pager.getStart());
		ps.addValue("size", pager.getPageSize());
		List<Map<String, Object>> list = namedJdbcTemplate.queryForList(sb.toString(), ps);

		pager.setRowCount(count);
		return list;
	}
	
	/**
	 * 查询管理员模块
	 * @return
	 */
	public List<Module> getAdminModule(int adminId){
		List<Module> list = getMapper().getAdminModule(adminId);
		return list;  		
	}
}
